<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:bf="http://www.bssmanager.com/jsf"
	template="/WEB-INF/layouts/standard.xhtml" xmlns:a4j="http://richfaces.org/a4j" xmlns:rich="http://richfaces.org/rich" xmlns:fn="http://www.bss.com/functions"
	xmlns:c="http://java.sun.com/jstl/core">


	<ui:define name="headIncludes">
		<style>			
			
		
			.rc {
				margin:0px;
				border:0px;
				
			}			
			.cc {
				width:120px; 
				height: 60px; 
				border:0px;
				
			}
			
			.ccInner {
				width:100%;
				height: 100%;
				font-size: 8pt;
			}
			.btnImgStyle {
				width: 25px; 
				height: 25px;
			}

			.ht1 {
				color: white;
				font-weight: bold;
				font-size: 8pt;
				width: 20px;
				margin-right: 15px;
			}
			.link1 {
				color: white !important;
				font-weight: bold;
				text-decoration: none;
			}
			
			
		</style>
	</ui:define>
	
	<ui:define name="formheader">
		<h2>Room Occupancy Manager</h2>
	</ui:define>

	<ui:define name="content">

		<h:messages />
		<h:form id="roomOccForm" prependId="false">


			<h:inputHidden id="selectedPatientId" value="#{roomOccupancyController.selectedPatientId}"/>
			<h:inputHidden id="selectedRoomId" value="#{roomOccupancyController.roomBooking.room.id}"/>
			<h:inputHidden id="selectedBedId" value="#{roomOccupancyController.bedBooking.bed.id}"/>
		
			<h3 align="center">ROOMS</h3>
			<hr />
			<h:panelGroup id="roomSelectorPanel">
			
				
			<ui:repeat var="building" value="#{roomOccupancyController.buildings}" >
				<h4 align="center">Building #{building.name}</h4>
				<hr/>
				<ui:repeat var="floor" value="#{arrayCreator[building.floors]}" >				
					<h5 align="center">Floor #{floor}</h5>
						<rich:dataGrid value="#{fn:call(roomOccupancyController,'findRooms',fn:concat(fn:concat(fn:concat(fn:concat('building.id=',building.id),',floor='),floor),',dormitory=false'))}" 
						var="room"  columns="5" rowClasses="rc" columnClasses="cc" style="border:0px" >							
								<h:panelGrid style="background-color: #{room.occupied ? 'gray':'green'};" styleClass="ccInner"  columns="1" >									
										<h:panelGrid columns="2">
											<h:outputText value="#{room.roomNumber}" styleClass="ht1"/>
											<a4j:commandButton rendered="#{not room.occupied}" styleClass="btnImgStyle"
													reRender="selectedRoomPanel" image="/images/key-small.png" limitToList="true"
													onclick="document.getElementById('selectedRoomId').value='#{room.id}';showLoading();"
													oncomplete="hideLoading();Richfaces.showModalPanel('selectedRoomPanel');"
													action="#{roomOccupancyController.selectRoom}" />
												
											<a4j:commandLink rendered="#{room.occupied}" styleClass="link1" limitToList="true"
												reRender="roomAllocationDetailPanel" value="#{fn:call(roomDao,'findCurrent',room).name}"
												onclick="document.getElementById('selectedRoomId').value='#{room.id}';showLoading();"
												oncomplete="hideLoading();Richfaces.showModalPanel('roomAllocationDetailPanel');" 
												action="#{roomOccupancyController.selectRoom}" />	
											
											
										</h:panelGrid>									
								</h:panelGrid>													
						</rich:dataGrid>
						
				</ui:repeat>
			</ui:repeat>
			</h:panelGroup>
			
			<h3 align="center">DORMITORIES</h3>
			<hr />	
			<h:panelGroup id="bedSelectorPanel">
			
			<ui:repeat var="building" value="#{roomOccupancyController.buildings}" >
				<h4 align="center">Building #{building.name}</h4>
				<hr/>
				<ui:repeat var="floor" value="#{arrayCreator[building.floors]}" >				
					<h5 align="center">Floor #{floor}</h5>	
					<ui:repeat var="dormitory" value="#{fn:call(roomOccupancyController,'findRooms',fn:concat(fn:concat(fn:concat(fn:concat('building.id=',building.id),',floor='),floor),',dormitory=true'))}" >
						<h4>Dormitory-${dormitory.roomNumber}</h4>
						<rich:dataGrid value="#{fn:asList(dormitory.beds)}" var="bed" columns="5" style="border:0px" rowClasses="rc" columnClasses="cc">
							
								<h:panelGrid columns="1" style="background-color: #{bed.occupied ? 'gray':'green'};"  styleClass="ccInner" >									
									<h:panelGrid columns="2">									
										<h:outputText value="#{bed.bedNumber}" styleClass="ht1"/>									
										<a4j:commandButton rendered="#{not bed.occupied}" styleClass="btnImgStyle"
											reRender="selectedBedPanel" image="/images/key-small.png" limitToList="true"
											onclick="document.getElementById('selectedBedId').value='#{bed.id}';showLoading();"
											oncomplete="hideLoading();Richfaces.showModalPanel('selectedBedPanel');"
											action="#{roomOccupancyController.selectBed}" />
										<a4j:commandLink rendered="#{bed.occupied}" 
											reRender="bedAllocationDetailPanel" value="#{fn:call(bedDao,'findCurrent',bed).name}"
											styleClass="link1" limitToList="true"
											onclick="document.getElementById('selectedBedId').value='#{bed.id}';showLoading();"
											oncomplete="hideLoading();Richfaces.showModalPanel('bedAllocationDetailPanel');"
											action="#{roomOccupancyController.selectBed}" />										
									</h:panelGrid>
								</h:panelGrid>								
																	
						</rich:dataGrid>							
					</ui:repeat>
				</ui:repeat>
			</ui:repeat>
			</h:panelGroup>

		</h:form>
		
		<rich:modalPanel id="selectedRoomPanel" minHeight="100" minWidth="200" autosized="true"
			>
			<f:facet name="header">
				<h:outputText value="Room Allocator"/>
			</f:facet>
			<div class="modalPanelInternal">				
				<h:form prependId="false">	
					<h:panelGrid columns="2">
						<h:outputLabel value="Selected Room:"/>
						<h:outputText value="Building: #{roomOccupancyController.roomBooking.room.building.name}, Floor:#{roomOccupancyController.roomBooking.room.floor}, Room Number: #{roomOccupancyController.roomBooking.room.roomNumber}" style="font-weight: bold;"/>
					</h:panelGrid>
					<h:panelGroup>
							<bf:admissionSelector idd="rpSelector" controller="#{roomOccupancyController}" selectMethod="select" reRender="selectedRoomOwner1,selectedRoomOwner2,btnAR"/>
						</h:panelGroup>
					<rich:panel style="border:0">
						<f:facet name="header">
							<h:outputText value="Bookings"/>
						</f:facet>
								<h:panelGrid columns="2" id="selectedRoomOwner1">
									<h:outputLabel value="Name: "/>
									<h:outputText style="color:blue; font-weight:bold;" value="#{roomOccupancyController.roomBooking.admission.patient.id ne 0 ? roomOccupancyController.roomBooking.admission.patient.name : 'Not Selected! Please search a patient' }"/>
								</h:panelGrid>
								<h:panelGroup id="selectedRoomOwner2">
									<h:outputLabel value="Previous Bookings:"/>
									<h:panelGrid columns="2" border="1">										
										<h:outputLabel value="Rooms:"/>
										<h:outputLabel value="Beds:"/>
										<rich:dataList value="#{roomOccupancyController.roomBooking.admission.roomBookings}" var="roomBooking">
											<h:outputText value="#{roomBooking.room.roomNumber}"/>
										</rich:dataList>																		
										<rich:dataList value="#{roomOccupancyController.bedBooking.admission.bedBookings}" var="bedBooking">
											<h:outputText value="#{bedBooking.bed.bedNumber}"/>
										</rich:dataList>										
									</h:panelGrid>
								</h:panelGroup>
					</rich:panel>
					
					<a4j:commandButton id="btnAR" disabled="#{roomOccupancyController.roomBooking.admission.id le 0}"
								action="#{roomOccupancyController.allocateRoom}" 
								value="Allocate Selected Room" limitToList="true"
								onclick="if(!showConfirmation('Are you sure to allocate the room ?')) return false;showLoading();"
								oncomplete="hideLoading();Richfaces.hideModalPanel('selectedRoomPanel');"
								reRender="roomSelectorPanel,suggestion-rpSelector"/>
					<a4j:commandButton value="Cancel" onclick="showLoading();"
								oncomplete="hideLoading();Richfaces.hideModalPanel('selectedRoomPanel'); "
								action="#{roomOccupancyController.cancelBooking}" limitToList="true"
								reRender="selectedRoomPanel,suggestion-rpSelector"/>			
																
				</h:form>
			</div>
		</rich:modalPanel>
		
		
		<rich:modalPanel id="selectedBedPanel" minHeight="100" minWidth="150"
			 autosized="true">
			<f:facet name="header">
				<h:outputText value="Bed Allocator"/>
			</f:facet>
			<div class="modalPanelInternal">				
				<h:form>	
					<h:panelGrid columns="2">
						<h:outputLabel value="Selected Bed:"/>
						<h:outputText value="#{roomOccupancyController.bedBooking.bed.bedNumber} " style="font-weight: bolder;"/>
					</h:panelGrid>
					<h:panelGroup>
						<bf:admissionSelector idd="bpSelector" controller="#{roomOccupancyController}" selectMethod="select" reRender="selectedBedOwner1,selectedBedOwner2,btnAB"/>
					</h:panelGroup>
					<rich:panel style="border:0">
						<f:facet name="header">
							<h:outputText value="Bookings"/>
						</f:facet>
						<h:panelGrid columns="2" id="selectedBedOwner1">
									<h:outputLabel value="Name: "/>
									<h:outputText style="color:blue; font-weight:bold;" value="#{roomOccupancyController.bedBooking.admission.patient.name}"/>
								</h:panelGrid>
								<h:panelGroup id="selectedBedOwner2">
									<h:outputLabel value="Previous Bookings:"/>
									<h:panelGrid columns="2" border="1">
										<h:outputLabel value="Rooms:"/>
										<h:outputLabel value="Beds:"/>										
										<rich:dataList value="#{roomOccupancyController.roomBooking.admission.roomBookings}" var="roomBooking">
											<h:outputText value="#{roomBooking.room.roomNumber}"/>
										</rich:dataList>
										<rich:dataList value="#{roomOccupancyController.bedBooking.admission.bedBookings}" var="bedBooking">
											<h:outputText value="#{bedBooking.bed.bedNumber}"/>
										</rich:dataList>										
									</h:panelGrid>
								</h:panelGroup>
					</rich:panel>	
					<a4j:commandButton id="btnAB" disabled="#{roomOccupancyController.bedBooking.admission.id le 0}"
									action="#{roomOccupancyController.allocateBed}" 
									value="Allocate Selected Bed" limitToList="true"
									onclick="if(!showConfirmation('Are you sure to allocate the bed ?')) return false;showLoading();"
									oncomplete="hideLoading();Richfaces.hideModalPanel('selectedBedPanel');"
									reRender="bedSelectorPanel"/>
					<a4j:commandButton value="Cancel" onclick="showLoading();" limitToList="true"
									oncomplete="hideLoading();Richfaces.hideModalPanel('selectedBedPanel'); "
									action="#{roomOccupancyController.cancelBooking}"
									reRender="selectedBedPanel"/>
				</h:form>
			</div>
		</rich:modalPanel>

		<rich:modalPanel id="roomAllocationDetailPanel" minHeight="100" minWidth="150"
			 autosized="true">
			<f:facet name="header">
				<h:outputText value="Room Allocation Details"/>
			</f:facet>
			<div class="modalPanelInternal">				
				<h:form>
					<h:panelGrid columns="2">
						<h:outputLabel value="Room Number: "/>
						<h:outputText value="#{roomOccupancyController.roomBooking.room.roomNumber}"/>						
					</h:panelGrid>
														
					<h:panelGrid columns="2">
								<a4j:commandButton 
									action="#{roomOccupancyController.deallocateRoom}" 
									value="Deallocate"
									onclick="if(!showConfirmation('Are you sure to vacate the room #{roomOccupancyController.roomBooking.room.roomNumber} ?')) return false;showLoading();"
									oncomplete="hideLoading();Richfaces.hideModalPanel('roomAllocationDetailPanel');"
									reRender="roomSelectorPanel"/>
								<a4j:commandButton value="Close"
									onclick="Richfaces.hideModalPanel('roomAllocationDetailPanel');return false;"/>
					</h:panelGrid>					
				</h:form>
			</div>
		</rich:modalPanel>
		
		<rich:modalPanel id="bedAllocationDetailPanel" minHeight="100" minWidth="150"
			 autosized="true">
			<f:facet name="header">
				<h:outputText value="Bed Allocation Details"/>
			</f:facet>
			<div class="modalPanelInternal">				
				<h:form>
					<h:panelGrid columns="2">
						<h:outputLabel value="Bed Number: "/>
						<h:outputText value="#{roomOccupancyController.bedBooking.bed.bedNumber}"/>						
					</h:panelGrid>
														
					<h:panelGrid columns="2">
								<a4j:commandButton 
									action="#{roomOccupancyController.deallocateBed}" 
									value="Deallocate"
									onclick="if(!showConfirmation('Are you sure to vacate the bed #{roomOccupancyController.bedBooking.bed.bedNumber} ?')) return false;showLoading();"
									oncomplete="hideLoading();Richfaces.hideModalPanel('bedAllocationDetailPanel');"
									reRender="bedSelectorPanel"/>
								<a4j:commandButton value="Close"
									onclick="Richfaces.hideModalPanel('bedAllocationDetailPanel');return false;"/>
					</h:panelGrid>					
				</h:form>
			</div>
		</rich:modalPanel>

	</ui:define>
</ui:composition>